package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.*;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class FIDao {

	DBHelp<Ffile> db=new DBHelp<Ffile>();
	
	public void save(Ffile f){
		String sql="insert into t_file(id,finame,filename,createtime,filesize,employeeid,folderid) values(?,?,?,?,?,?,?)";
		db.executeSQL(sql,f.getId(),f.getFiname(),f.getFilename(),f.getCreatetime(),f.getFilesize(),f.getEmployeeid(),f.getFolderid());
	}
	
	public List<Ffile> findByFolderid(String folderid){
		String sql="select * from t_file where folderid=? order by createtime";
		return db.queryForList(sql,new FIRowMapper(),folderid);
	}
	
	public int count1ByFolderid(String folderid){
		List<Ffile> filist=findByFolderid(folderid);
		int total=filist.size();
		return total;
	}
	
	public double count2ByFolderid(String folderid){
		List<Ffile> filist=findByFolderid(folderid);
		double total=0.0;
		for(Ffile fi: filist){
			total+=fi.getFilesize();
		}
		return total;
	}
	
	private class FIRowMapper implements RowMapper<Ffile>{

		public Ffile mapperRow(ResultSet rs) throws SQLException {
			
			Ffile file=new Ffile();
			file.setCreatetime(rs.getString("createtime"));
			file.setEmployeeid(rs.getInt("employeeid"));
			file.setFilename(rs.getString("filename"));
			file.setFilesize(rs.getDouble("filesize"));
			file.setFiname(rs.getString("finame"));
			file.setFolderid(rs.getString("folderid"));
			file.setId(rs.getString("id"));
			return file;
			
		}
		
	}
}
